package com.bootdo.xzl.service.impl;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.domain.DictDO;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.UploadUtils;
import com.bootdo.system.domain.UserDO;
import com.bootdo.xzl.dao.HousesImgInfoDao;
import com.bootdo.xzl.dao.HousesInfoDao;
import com.bootdo.xzl.domain.HousesImgInfoDO;
import com.bootdo.xzl.domain.HousesImgInfoDO2;
import com.bootdo.xzl.domain.HousesInfoDO;
import com.bootdo.xzl.service.HousesInfoService;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

@Service
public class HousesInfoServiceImpl implements HousesInfoService {

	@Autowired
	private HousesInfoDao housesInfoDao;
	
	@Autowired
	private HousesImgInfoDao housesImgInfoDao;
	
	@Value("${file.path}")
	private String filePath;

	@Override
	public HousesInfoDO get(Integer id) {
		return housesInfoDao.get(id);
	}

	@Override
	public List<HousesInfoDO> list(Map<String, Object> map) {
		
		List<HousesInfoDO> list = housesInfoDao.list(map);
		if(null != list && list.size()>0){
			int max = list.size();
			for(int i=0; i<max; i++){ 
				list.get(i).setImgFlg("");
				//租金显示
				if(list.get(i).getRentMethod().equals("2")){
						list.get(i).setMoney(list.get(i).getMoneyMonth());
				}
				
				List<HousesImgInfoDO> imageList =  housesImgInfoDao.getImgList(list.get(i));
					if(null != imageList && imageList.size()>0){
						list.get(i).setHousesImgInfoList(imageList);
					}
					
					
					if(StringUtils.isNotEmpty(list.get(i).getFoundationMatching())){
						 
						String[] values = list.get(i).getFoundationMatching().split(","); 
						
						List<DictDO> dictFoundationMatchingList = housesInfoDao.dictFoundationMatchingList(values);
						 
						if(null != dictFoundationMatchingList && dictFoundationMatchingList.size()>0){
							 
							list.get(i).setFoundationMatchingList(dictFoundationMatchingList);
						 }
					}
			}
		}
		return list;
	}

	@Override
	public int count(Map<String, Object> map) {
		return housesInfoDao.count(map);
	}

	@Override
	public int save(HousesInfoDO housesInfo){
		String indexradioFile="";
		try {
						// 生成房源编号（）
						housesInfo.setOfficeHousesNumber(generatePrimarykey());
						
						//出售
						if(housesInfo.getOfficeProperty().equals("1")){
							//金额判断
							 if(null !=housesInfo && StringUtils.isNotEmpty(housesInfo.getRentMethod())){
								  // 天
								  if("1".equals(housesInfo.getRentMethod())){
										//出售价格
									  	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
									  	 int builtArea = housesInfo.getBuiltArea();
									     moneyMonth = moneyMonth.multiply(new BigDecimal(builtArea));
									     DecimalFormat df =new DecimalFormat("0");
									     String str = df.format(moneyMonth);
									     housesInfo.setMoneyMonth(new BigDecimal(str));
									     //housesInfo.setMoneyMonth(str);
								  }
								  // 月
								  if("2".equals(housesInfo.getRentMethod())){
										///出售价格
									   	 housesInfo.setMoneyMonth(housesInfo.getMoney());
									   	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
									   	 int builtArea = housesInfo.getBuiltArea();								   
									     BigDecimal moneyB = moneyMonth.divide(new BigDecimal(builtArea), 2, RoundingMode.HALF_DOWN);
									     DecimalFormat df =new DecimalFormat("0.00");
									     String money = df.format(moneyB);
									     housesInfo.setMoney(new BigDecimal(money));
									     //housesInfo.setMoney(money);
								  }
							 }  
							
						}
						//出租
						if(housesInfo.getOfficeProperty().equals("0")){
							//金额判断
							 if(null !=housesInfo && StringUtils.isNotEmpty(housesInfo.getRentMethod())){
								  // 天
								  if("1".equals(housesInfo.getRentMethod())){
										//月租金
									  	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
									  	 int builtArea = housesInfo.getBuiltArea();
									  	 BigDecimal month = new BigDecimal(30);
									     moneyMonth = moneyMonth.multiply(month).multiply(new BigDecimal(builtArea));
									     DecimalFormat df =new DecimalFormat("0");
									     String str = df.format(moneyMonth);
									     housesInfo.setMoneyMonth(new BigDecimal(str));
									     //housesInfo.setMoneyMonth(str);
								  }
								  // 月
								  if("2".equals(housesInfo.getRentMethod())){
										//日租金
									   	 housesInfo.setMoneyMonth(housesInfo.getMoney());
									   	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
									   	 int builtArea = housesInfo.getBuiltArea();
									   	 BigDecimal month = new BigDecimal(30);
									     BigDecimal moneyB = moneyMonth.divide(month, 2, RoundingMode.HALF_DOWN).divide(new BigDecimal(builtArea), 2, RoundingMode.HALF_DOWN);
									     //BigDecimal y = new BigDecimal(10000);
									     DecimalFormat df =new DecimalFormat("0.00");
									     String money = df.format(moneyB);
									     housesInfo.setMoney(new BigDecimal(money));
									     //housesInfo.setMoney(money);
								  }
							 }  
						}
						//根据索引获取文件名
						 // 获取关联文件
						 List<MultipartFile> files =  housesInfo.getFiles();
						 if(files!=null) {
							 if("".equals(housesInfo.getIndexradio())) {
								 housesInfo.setIndexradio("0");
								 
								 indexradioFile = afterUploadFileName(files.get(0).getOriginalFilename());
							 }else {
								 indexradioFile = afterUploadFileName(files.get(Integer.parseInt(housesInfo.getIndexradio())).getOriginalFilename());
							 }		
							housesInfo.setIndexfile(indexradioFile);
						 }else {
							 housesInfo.setIndexradio("");
							 housesInfo.setIndexfile("");
						 }
			
					housesInfoDao.save(housesInfo);	
			       if(null!=housesInfo && null!=housesInfo.getFiles() && housesInfo.getFiles().size()>0){
					
						 int max = files.size();
						 List<HousesImgInfoDO> images =  new ArrayList<>(max);
						 HousesImgInfoDO housesImgInfoDO =  null;
	
					 for(int i=0;i<max;i++){
						         
								 housesImgInfoDO = new HousesImgInfoDO();
								 // 房源id
								 housesImgInfoDO.setOfficeId(housesInfo.getId());
								 // 房源编号
								 housesImgInfoDO.setOfficeHousesNumber(housesInfo.getOfficeHousesNumber());
								 // 图片编号
								 housesImgInfoDO.setImgNo(generatePrimarykey());
								 // 上传文件名
								 housesImgInfoDO.setFileName(files.get(i).getOriginalFilename());
								 // 磁盘物理文件名
								 String physicalFileName = afterUploadFileName(files.get(i).getOriginalFilename());
								 if(i==Integer.parseInt(housesInfo.getIndexradio())) {
									 housesImgInfoDO.setPhysicalFileName(indexradioFile);
								 }else {
									 housesImgInfoDO.setPhysicalFileName(physicalFileName);
								 }
				
								 // 上传人
								 if(null!=housesInfo.getUserId()){
									 housesImgInfoDO.setCreateBy(String.valueOf(housesInfo.getUserId()));
								 }
								 // 文件类型
								 String filetype = getSuffixName(files.get(i).getOriginalFilename());
								 housesImgInfoDO.setFileType(filetype);
								 //文件大小
								 housesImgInfoDO.setFileSize(files.get(i).getSize());
								// 获取水印图片
								InputStream inputImg = files.get(i).getInputStream();
								BufferedImage image = ImageIO.read(inputImg);

								 Thumbnails.of(image).size(800,600).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(filePath+"logo.png")), 0.4f)
							        .outputQuality(0.6f).toFile(filePath+housesImgInfoDO.getPhysicalFileName());
			
								// 添加集合
								images.add(housesImgInfoDO);
					 }	 					 
				 housesImgInfoDao.save(images);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	@Override
	public int update(HousesInfoDO housesInfo) {
		 // 获取关联文件
		 List<MultipartFile> files =null;
		 List<HousesImgInfoDO> images =null;
		 List<HousesImgInfoDO2> lstimages = new ArrayList<HousesImgInfoDO2>();
		 HousesImgInfoDO2 housesImgInfoDO2 =null;
		 for(int i=0;i<housesInfo.getComonefiles().size();i++) {
			 
			 housesImgInfoDO2 = JSON.parseObject(housesInfo.getComonefiles().get(i), HousesImgInfoDO2.class);
			 lstimages.add(housesImgInfoDO2);
		 }
		 
		try {
			
			// 删除磁盘文件
			if(null!=housesInfo && housesInfo.getDelImagesIds()!=null && housesInfo.getDelImagesIds().size()>0){
					// 查询文件列表
				   Integer[] array =  housesInfo.getDelImagesIds().toArray(new  Integer[housesInfo.getDelImagesIds().size()]);
				   images = housesImgInfoDao.getImgListByIds(array);
				   if(null!=images && images.size()>0 ){
					    int max = images.size();
					    // 删除磁盘文件
					    File file  = null;
					   for(int i=0;i<max;i++){
						   file = new File(filePath+images.get(i).getPhysicalFileName());
						   if(file.exists()){
							   file.delete();
						   }
					   }
				   }
				 //删除数据库文件

				 housesImgInfoDao.batchUpdate(images);
			}
			
			// 上传文件
			if(null!=housesInfo && null!=housesInfo.getFiles() && housesInfo.getFiles().size()>0){
					 // 获取关联文件
					 files =  housesInfo.getFiles();
					 int max = files.size();
					 images =  new ArrayList<>(max);
					 HousesImgInfoDO housesImgInfoDO =  null;
				
				 for(int i=0;i<max;i++){
							 housesImgInfoDO = new HousesImgInfoDO();
							 // 房源id
							 housesImgInfoDO.setOfficeId(housesInfo.getId());
							 // 房源编号	
							 housesImgInfoDO.setOfficeHousesNumber(generatePrimarykey());
							 // 图片编号
							 housesImgInfoDO.setImgNo(generatePrimarykey());
							 // 上传文件名
							 housesImgInfoDO.setFileName(files.get(i).getOriginalFilename());
							 // 磁盘物理文件名
							 String physicalFileName = afterUploadFileName(files.get(i).getOriginalFilename());
							 housesImgInfoDO.setPhysicalFileName(physicalFileName);
							 // 上传人
							 if(null!=housesInfo.getUserId()){
								 housesImgInfoDO.setCreateBy(String.valueOf(housesInfo.getUserId()));
							 }
							 // 文件类型
							 String filetype = getSuffixName(files.get(i).getOriginalFilename());
							 housesImgInfoDO.setFileType(filetype);
							 //文件大小
							 housesImgInfoDO.setFileSize(files.get(i).getSize());
							 // 上传文件
							// 获取水印图片
							InputStream inputImg = files.get(i).getInputStream();
							BufferedImage image = ImageIO.read(inputImg);

							 Thumbnails.of(image).size(800,600).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(filePath+"logo.png")), 0.4f)
						        .outputQuality(0.6f).toFile(filePath+housesImgInfoDO.getPhysicalFileName());
							// 添加集合
							images.add(housesImgInfoDO);
				 }
				 housesImgInfoDao.save(images);
			}
			
			//出售
			if(housesInfo.getOfficeProperty().equals("1")){
				//金额判断
				 if(null !=housesInfo && StringUtils.isNotEmpty(housesInfo.getRentMethod())){
					  // 天
					  if("1".equals(housesInfo.getRentMethod())){
							//出售价格
						  	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
						  	 int builtArea = housesInfo.getBuiltArea();
						     moneyMonth = moneyMonth.multiply(new BigDecimal(builtArea));
						     DecimalFormat df =new DecimalFormat("0");
						     String str = df.format(moneyMonth);
						     housesInfo.setMoneyMonth(new BigDecimal(str));
						     //housesInfo.setMoneyMonth(str);
					  }
					  // 月
					  if("2".equals(housesInfo.getRentMethod())){
							///出售价格
						   	 housesInfo.setMoneyMonth(housesInfo.getMoney());
						   	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
						   	 int builtArea = housesInfo.getBuiltArea();								   
						     BigDecimal moneyB = moneyMonth.divide(new BigDecimal(builtArea), 2, RoundingMode.HALF_DOWN);
						     //BigDecimal y = new BigDecimal(10000);
						     DecimalFormat df =new DecimalFormat("0.00");
						     String money = df.format(moneyB);
						     housesInfo.setMoney(new BigDecimal(money));
						     //housesInfo.setMoney(money);
					  }
				 }  
				
			}
			//出租
			if(housesInfo.getOfficeProperty().equals("0")){
				//金额判断
				 if(null !=housesInfo && StringUtils.isNotEmpty(housesInfo.getRentMethod())){
					  // 天
					  if("1".equals(housesInfo.getRentMethod())){
							//月租金
						  	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
						  	 int builtArea = housesInfo.getBuiltArea();
						  	 BigDecimal month = new BigDecimal(30);
						     moneyMonth = moneyMonth.multiply(month).multiply(new BigDecimal(builtArea));
						     DecimalFormat df =new DecimalFormat("0");
						     String str = df.format(moneyMonth);
						     housesInfo.setMoneyMonth(new BigDecimal(str));
						     //housesInfo.setMoneyMonth(str);
					  }
					  // 月
					  if("2".equals(housesInfo.getRentMethod())){
							//日租金
						   	 housesInfo.setMoneyMonth(housesInfo.getMoney());
						   	 BigDecimal moneyMonth = new BigDecimal(String.valueOf(housesInfo.getMoney()));
						   	 int builtArea = housesInfo.getBuiltArea();
						   	 BigDecimal month = new BigDecimal(30);
						     BigDecimal moneyB = moneyMonth.divide(month, 2, RoundingMode.HALF_DOWN).divide(new BigDecimal(builtArea), 2, RoundingMode.HALF_DOWN);
						     DecimalFormat df =new DecimalFormat("0.00");
						     String money = df.format(moneyB);
						     housesInfo.setMoney(new BigDecimal(money));
						     //housesInfo.setMoney(money);
					  }
				 }  
			}
			//根据索引获取文件名
            //1.没有删除文件操作只有上传文件操作。
			if(housesInfo.getDelImagesIds()==null && null!=housesInfo.getFiles() && housesInfo.getFiles().size()>0) {
				if(Integer.parseInt(housesInfo.getDatalength())>=Integer.parseInt(housesInfo.getBeforedatalength())) {
					if(!"".equals(housesInfo.getIndexradio())) {
						Integer c =Integer.parseInt(housesInfo.getIndexradio())-Integer.parseInt(housesInfo.getBeforedatalength());
						String filename = images.get(c).getPhysicalFileName();
						housesInfo.setIndexfile(filename);
					}else {
						housesInfo.setIndexradio("0");
						String filename = images.get(0).getPhysicalFileName();
						housesInfo.setIndexfile(filename);
					}
				}
			}
            //2.没有上传文件操作只有删除文件操作
			if(housesInfo.getDelImagesIds()!=null &&null==housesInfo.getFiles()) {
				if(Integer.parseInt(housesInfo.getIndexradio())>=Integer.parseInt(housesInfo.getDatalength())) {
				
					Integer c =Integer.parseInt("0");
					String filename = lstimages.get(c).getPhysicalFileName();
					housesInfo.setIndexradio("0");
					housesInfo.setIndexfile(filename);
				}else {
					Integer c =Integer.parseInt(housesInfo.getIndexradio());
					String filename = lstimages.get(c).getPhysicalFileName();
					housesInfo.setIndexfile(filename);
				}
			}
            //3.既有删除文件操作又有上传文件操作。
			if(housesInfo.getDelImagesIds()!=null && null!=housesInfo.getFiles() && housesInfo.getFiles().size()>0) {

					Integer c =Integer.parseInt(housesInfo.getIndexradio())-(Integer.parseInt(housesInfo.getBeforedatalength())-housesInfo.getDelImagesIds().size());
					String filename = images.get(c).getPhysicalFileName();
					housesInfo.setIndexfile(filename);

			}
			 // 修改人
			 if(null!=housesInfo.getUserId()){
				 housesInfo.setUpdateBy(String.valueOf(housesInfo.getUserId()));
			 }
			 housesInfo.setUpdateDate(DateUtils.getSysDate());
			 // 更新信息
			 housesInfoDao.update(housesInfo);
			 	
	} catch (Exception e) {
		e.printStackTrace();
		return 0;
	}
	return 1;
}

	@Override
	public int remove(Integer id) {
		
		// 删除关联图片
		   Map<String,Object> map = new HashMap<String, Object>(1);
		   map.put("officeId", id);
		   List<HousesImgInfoDO> images = housesImgInfoDao.list(map);
		   if(null!=images && images.size()>0 ){
			    int max = images.size();
			    // 删除磁盘文件
			    File file  = null;
			   for(int i=0;i<max;i++){
				   file = new File(filePath+images.get(i).getPhysicalFileName());
				   if(file.exists()){
					   file.delete();
				   }
			   }
			   // 删除数据库文件

			   housesImgInfoDao.batchUpdate(images);
		   }
		   
		   HousesInfoDO housesInfoDO = new HousesInfoDO();
		   housesInfoDO.setId(id);
		   housesInfoDO.setDelFlag("1");
			 // 修改人
		     // 用户信息
			UserDO user = ShiroUtils.getUser();
			 if(null!=user.getUserId()){
				 housesInfoDO.setUpdateBy(user.getUserId()+"");
			 }
		   housesInfoDO.setUpdateDate(DateUtils.getSysDate());
		return housesInfoDao.update(housesInfoDO);
	}

	@Override
	public int batchRemove(Integer[] ids) {
		
		// 删除关联图片
		   List<HousesImgInfoDO> images = housesImgInfoDao.getImgListByofficeId(ids,null);
		   if(null!=images && images.size()>0 ){
			    int max = images.size();
			    // 删除磁盘文件
			    File file  = null;
			   for(int i=0;i<max;i++){
				   file = new File(filePath+images.get(i).getPhysicalFileName());
				   if(file.exists()){
					   file.delete();
				   }
			   }
			   // 删除数据库文件
			   housesImgInfoDao.batchUpdate(images);
		   }
		   
		   List<HousesInfoDO> list = new ArrayList<>(ids.length);
		   HousesInfoDO housesInfoDO = null;
		   for(Integer id  : ids){
			   	housesInfoDO = new HousesInfoDO();
			   	housesInfoDO.setId(id);
			    housesInfoDO.setDelFlag("1");
			    list.add(housesInfoDO);
		   }
		return housesInfoDao.batchUpdate(list);
	}

	// 房源编号
	public String generatePrimarykey() {
		long r = (long) ((Math.random() * 9 + 1) * 10000000);
		long s = Calendar.getInstance().getTimeInMillis();
		String key = String.valueOf(r) + String.valueOf(s);
		return key;
	}

	// 获取上传后的文件名
	public String afterUploadFileName(String type) {
		if (StringUtils.isEmpty(type)) {
			return "";
		}
		long r = (long) ((Math.random() * 9 + 1) * 100000);
		long s = Calendar.getInstance().getTimeInMillis();
		String t = String.valueOf(r) + String.valueOf(s) + UploadUtils.getSuffixName(type);
		return t;
	}
	
	
	   //获取文件后缀名
		public String getSuffixName(String fileName) {
			if (StringUtils.isEmpty(fileName)) {
				return "";
			} else if (fileName.indexOf(fileName) < 0) {
				return "";
			}
			String suffix = fileName.substring(fileName.lastIndexOf("."));
			return suffix;
		}
		
		
		
		@Override
		public List<DictDO> dict(Map<String, Object> map) {
			
				
				return housesInfoDao.dict(map);
		}
		
		
		@Override
		public List<HousesImgInfoDO> imagesByofficeId(Map<String, Object> map) {
			
			return housesInfoDao.imagesByofficeId(map);
		}

		
		// 详情接口
		@Override
		public HousesInfoDO housesInfo(Integer id) {
			
			HousesInfoDO housesInfoDO = housesInfoDao.housesInfo(id);
			
			if(null!=housesInfoDO){
					// 查询文件列表
				   Integer[] array =  new Integer[1];
				   array[0] = housesInfoDO.getId();
				   List<HousesImgInfoDO> images = housesImgInfoDao.getImgListByofficeId(array,null);
				   //  添加图片
				   if(images!=null && images.size()>0){
					   housesInfoDO.setHousesImgInfoList(images);
				   }
				   
				   if(housesInfoDO!=null &&housesInfoDO.getPurpose().equals("1")){
					// 配套
					   if(StringUtils.isNotEmpty(housesInfoDO.getFoundationMatching()) && housesInfoDO.getFoundationMatching().contains(",")){
						    String[] values = housesInfoDO.getFoundationMatching().split(","); 
					   		List<DictDO> dictFoundationMatchingList = housesInfoDao.dictFoundationMatchingList(values);
					   		if(null != dictFoundationMatchingList && dictFoundationMatchingList.size()>0){
					   			// 添加配套
					   			housesInfoDO.setFoundationMatchingList(dictFoundationMatchingList);
					  		}
					   }
				   }
				   
				   if(housesInfoDO!=null &&housesInfoDO.getPurpose().equals("2")){
						// 配套
						   if(StringUtils.isNotEmpty(housesInfoDO.getFoundationMatching()) && housesInfoDO.getFoundationMatching().contains(",")){
							    String[] values = housesInfoDO.getFoundationMatching().split(","); 
						   		List<DictDO> dictFoundationMatchingList = housesInfoDao.dictShopsList(values);
						   		if(null != dictFoundationMatchingList && dictFoundationMatchingList.size()>0){
						   			// 添加配套
						   			housesInfoDO.setFoundationMatchingList(dictFoundationMatchingList);
						  		}
						   }
					   }
				   
				   
			}
			// 返回
			return housesInfoDO;
		}
		
		
		  /**
	     * 给图片添加水印文字、可设置水印文字的旋转角度
	     * @param logoText 要写入的文字
	     * @param srcImgPath 源图片路径
	     * @param newImagePath 新图片路径
	     * @param degree 旋转角度
	     * @param color  字体颜色
	     * @param formaName 图片后缀
	     */
	    public static void markImageByText(String logoText, BufferedImage srcImg,String newImagePath,Integer degree,Color color,String formaName) {
	        InputStream is = null;
	        OutputStream os = null;
	        try {
	            // 1、源图片
	            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
	            // 2、得到画笔对象
	            Graphics2D g = buffImg.createGraphics();
	            // 3、设置对线段的锯齿状边缘处理
	            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
	            // 4、设置水印旋转
	            if (null != degree) {
	                g.rotate(Math.toRadians(degree),  buffImg.getWidth()/2,buffImg.getHeight() /2);
	            }
	            // 5、设置水印文字颜色
	            g.setColor(color);
	            // 6、设置水印文字Font
	            g.setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, buffImg.getHeight() /2));
	            // 7、设置水印文字透明度
	            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.15f));
	            // 8、第一参数->设置的内容，后面两个参数->文字在图片上的坐标位置(x,y)
	            g.drawString(logoText,  buffImg.getWidth()/2 , buffImg.getHeight()/2);
	            // 9、释放资源
	            g.dispose();
	            // 10、生成图片
	            os = new FileOutputStream(new File(newImagePath));
	            ImageIO.write(buffImg, formaName, os);
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (null != is)
	                    is.close();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	            try {
	                if (null != os)
	                    os.close();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
	    }

		/**
		* 加文字水印  
		* @param bufImg --BufferedImage  用来画图的宽高跟需要加水印的图片一样的空白图
		* @param img --需要加水印的图片
		* @param text --水印文字
		* @param font --字体
		* @param color --颜色
		* @param x  --水印相对于底片的x轴坐标(PS:左上角为(0,0))
		* @param y  --水印相对于底片的y轴坐标(PS:左上角为(0,0))
		* @author 高永强 
		* @version 2018年11月30日 上午11:39:57
		 */
		public static void markWord(BufferedImage bufImg, Image img, String text, Font font, Color color, int x, int y) {
			//取到画笔
			Graphics2D g = bufImg.createGraphics();
			//画底片
			g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
			g.setColor(color);
			g.setFont(font);
			//位置
			g.drawString(text, x, y);
			g.dispose();
		}
}
